//
//  LoginViewController.m
//  Fish
//
//  Created by KING on 16/4/18.
//  Copyright © 2016年 huwo. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
//#import "QuartzCore/QuartzCore.h"
#import "ForPassViewController.h"
#import "Header.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) UITextField *userName;
@property (strong, nonatomic) UITextField *passWord;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *registButton;
@property (nonatomic, strong) UIButton *changeButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
//    [inputHelper setupInputHelperForView:self.view withDismissType:InputHelperDismissTypeTapGusture];

    
//    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 85)];
//    la.text = @"登录";
//    la.backgroundColor = [UIColor colorWithRed:55/255.0 green:58/255.0 blue:62/255.0 alpha:1];
//    la.textAlignment = NSTextAlignmentCenter;
//    la.textColor = [UIColor whiteColor];
//    [self.view addSubview:la];
    
//    self.view.backgroundColor = [UIColor colorWithRed:55/255.0 green:58/255.0 blue:62/255.0 alpha:1];
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    leftButton.frame = CGRectMake(20, 30, 30, 30);
//    [leftButton setImage:[UIImage imageNamed:@"nav_but_back"] forState:UIControlStateNormal];
//    [self.view addSubview:leftButton];
//    [leftButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.userName = [[UITextField alloc]initWithFrame:CGRectMake(kUIScreenWidth/2-140, 165, 280, 45)];
    self.userName.placeholder = @"  |请输入用户名";
    self.userName.textColor = [UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1];
//    self.userName.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.userName];
    self.userName.layer.borderColor=[[UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1] CGColor];
    self.userName.layer.borderWidth = 1.0;
    
    
   [self.userName addTarget:self action:@selector(textValueChanged) forControlEvents:UIControlEventEditingChanged];
    
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 21)];
    label.backgroundColor = [UIColor clearColor];
    self.userName.leftViewMode = UITextFieldViewModeAlways;
    self.userName.leftView = label;

    
    self.passWord = [[UITextField alloc ]initWithFrame:CGRectMake(kUIScreenWidth/2-140, 240, 280, 45)];
    self.passWord.placeholder = @"  |请输入密码";
    self.passWord.textColor = [UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1];
//    self.passWord.backgroundColor = [UIColor Color];
    [self.view addSubview:self.passWord];
    self.passWord.layer.borderColor = [[UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1] CGColor];
    self.passWord.layer.borderWidth = 1.0;
    //设置是否密文显示
    self.passWord.secureTextEntry = YES;

    
    
    UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 21)];
    label.backgroundColor = [UIColor clearColor];
    self.passWord.leftViewMode = UITextFieldViewModeAlways;
    self.passWord.leftView = blabel;
    
    UIImageView *passImage = [[UIImageView alloc] initWithFrame:CGRectMake(280-35, 12, 25, 18)];
    passImage.image = [UIImage imageNamed:@"main_icon_eye@2x"];
    [self.passWord addSubview:passImage];

    
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton.frame = CGRectMake(kUIScreenWidth/2-140, 315, 280, 45);
    self.loginButton.backgroundColor = [UIColor colorWithRed:166/255.0 green:168/255.0 blue:170/255.0 alpha:1];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
//    [self.loginButton setBackgroundImage:[UIImage imageNamed:@"main_btn_log"] forState:UIControlStateNormal];
    [self.view addSubview:self.loginButton];
    [self.loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registButton.frame = CGRectMake(kUIScreenWidth/2-140, 400, 40, 40);
    [self.registButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registButton setTitleColor:[UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1] forState:UIControlStateNormal];
    [self.view addSubview:self.registButton];
    [self.registButton addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];

    
    
    self.changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.changeButton.frame = CGRectMake(kUIScreenWidth/2-140+180, 400, 100, 40);
    [self.changeButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.changeButton setTitleColor:[UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1] forState:UIControlStateNormal];
    [self.view addSubview:self.changeButton];

    [self.changeButton addTarget:self action:@selector(forAction:) forControlEvents:UIControlEventTouchUpInside];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

//    [KeyboardToolBar registerKeyboardToolBar:self.userName];
//    [KeyboardToolBar registerKeyboardToolBar:self.passWord];


    self.navigationItem.title = @"登录";
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)forAction:(UIButton *)btn{
    ForPassViewController *pass = [[ForPassViewController alloc] init];
    [self.navigationController pushViewController:pass animated:YES];
}
//#pragma mark - 监听
//- (void)keyboardWillChangeFrame:(NSNotification *)note
//{
//    // 工具条平移的距离 == 屏幕高度 - 键盘最终的Y值
//    self.userName.constant = HEIGHT - [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
//    
//    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    [UIView animateWithDuration:duration animations:^{
//        [self.view layoutIfNeeded]; // 修改约束，执行动画，必须重写调整
//    }];
//}

-(void)loginAction:(UIButton *)btn{
    [self.passWord resignFirstResponder];
    [self.userName resignFirstResponder];
    [[NSUserDefaults standardUserDefaults] setObject:@"25" forKey:@"userid"];
    [self.navigationController popViewControllerAnimated:YES];
    [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"login"];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    NSDictionary *parameters = @{@"username":@"18624392626", @"password":@"111"};
//    
//    [manager POST:@"http://api03.m0571.com/WebJson.aspx?book=Login" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
////        NSString *str1 = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
////        NSLog(@"%@",str1);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@", error);
//    }];
//        NSString *url = [NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetType"];
//        AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//  
//        session.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//        session.responseSerializer = [AFJSONResponseSerializer serializer];
//        session.requestSerializer = [AFJSONRequestSerializer serializer];
//        [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//        [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//        session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
//        [session GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//    //
//    //
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            NSLog(@"===%@",responseObject);
//            NSArray *array = [NSArray arrayWithArray:responseObject];
//            NSMutableDictionary *dic = array[0];
//            NSString *string =[dic objectForKey:@"describe"];
//    
//            if ([string isEqualToString:@"注册成功"]) {
//    
//                NSLog(@"---%@",string);
//                //            [AppTools HUDAlert:self.view string:string];
//    
//    
//            }
//    
//            else{
//                NSLog(@"---%@",string);
//                //            [AppTools HUDAlert:self.view string:string];
//            }
//    
//    
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            NSLog(@"shibai");
//            
//        }];

    NSDictionary *dic = @{@"username":@"18624392626",
                          @"password":@"111"};
    [AppTools postURL:@"http://api03.m0571.com/WebJson.aspx?book=Login" data:dic
            success:^(id result) {
                NSLog(@"%@",result);
//                NSString *str1 = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
//                        NSLog(@"%@",str1);

    
          } error:^(NSError *error) {
              NSLog(@"%@",error);
    
    }];
}
//当属性值发送变化时触发的方法
- (void)textValueChanged{
    
    if (self.userName.text.length != 0) {
        [self.loginButton setBackgroundImage:[UIImage imageNamed:@"main_btn_login@2x"] forState:UIControlStateNormal];
    }
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.passWord resignFirstResponder];
    [self.userName resignFirstResponder];
}


//-(void)backAction:(UIButton *)nav{
//    
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//        
//    }];
//    
//}
-(void)registAction:(UIButton *)btn{
    RegistViewController *reVC = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:reVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
